import {
    createRouter,
    createWebHashHistory
} from 'vue-router';

import GLOBAL from '@/utils/global.js'

import {
    getLocalStorage
} from '@/utils/common'

import home from "@/components/home";
import login from "@/view/login";
import project from "@/view/project";

import pm from "@/view/pm/index";
import document from "@/view/pm/document";
import staff from "@/view/pm/staff";
import environment from "@/view/pm/environment";
import vehicle from "@/view/pm/vehicle";
import train from "@/view/pm/train";
import safety from "@/view/pm/safety";
import lean from "@/view/pm/lean";
import progress from "@/view/pm/progress";
import report from "@/view/pm/report";
import projectHome from "@/view/pm/projectHome";
import DailyReport from "@/components/report/DailyReport";
import WeekReport from "@/components/report/WeekReport";
import MonthReport from "@/components/report/MonthReport";
import workSurface from "@/view/pm/workSurface";
import lowCarbon from "@/view/pm/lowCarbon";

import bimIndex from "@/view/bim/index";
import bim from "@/view/bim/bim";
import roam from "@/view/bim/roam";

import pc from "@/view/pc/index";
import model from "@/view/pc/model";
import marker from "@/view/pc/marker";
import schedule from "@/view/pc/schedule";

import ibe from "@/view/ibe/index";
import machinery from "@/view/ibe/machinery";
import robot from "@/view/ibe/robot";
import idd from "@/view/idd/index";

import unpermission from "@/components/unpermission"

const routes = [{
    path: '/',
    component: home,
    name: '/',
    meta: {
        auth: true
    }
}, {
    path: '/home',
    name: 'home',
    component: home,
    meta: {
        auth: true
    }
}, {
    path: '/login',
    name: 'login',
    component: login
}, {
    path: '/project/:proName',
    name: 'project',
    component: project,
    meta: {
        auth: true
    },
    props: true,
    children: [{
        path: '/pm',
        component: pm,
        name: 'pm',
        meta: {
            auth: true
        },
        children: [
            {
                path: '/pm/projectHome',
                component: projectHome,
                name: 'projectName',
                meta: {
                    auth: true
                }
            }, {
                path: '/pm/document',
                component: document,
                name: 'document',
                meta: {
                    auth: true
                },
            },
            {
                path: '/pm/train',
                component: train,
                name: 'train',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/staff',
                component: staff,
                name: 'staff',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/environment',
                component: environment,
                name: 'environment',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/vehicle',
                component: vehicle,
                name: 'vehicle',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/safety',
                component: safety,
                name: 'safety',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/workSurface',
                component: workSurface,
                name: 'workSurface',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/lowCarbon',
                component: lowCarbon,
                name: 'lowCarbon',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/lean',
                component: lean,
                name: 'lean',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/progress',
                component: progress,
                name: 'progress',
                meta: {
                    auth: true
                }
            },
            {
                path: '/pm/report',
                component: report,
                name: 'report',
                meta: {
                    auth: true
                },
                children: [{
                    path: '/project/report/day:currentReportType:currentDate:reportId',
                    component: DailyReport,
                    name: 'DailyReport',
                    props: true,
                    meta: {
                        auth: true
                    }
                },
                {
                    path: '/project/report/week:currentReportType:currentDate:reportId',
                    component: WeekReport,
                    name: 'WeekReport',
                    props: true,
                    meta: {
                        auth: true
                    }
                },
                {
                    path: '/project/report/month:currentReportType:currentDate:reportId',
                    component: MonthReport,
                    name: 'MonthReport',
                    props: true,
                    meta: {
                        auth: true
                    }
                },
                ]
            }
        ]
    }, {
        path: '/bim',
        component: bimIndex,
        name: 'bimIndex',
        meta: {
            auth: true
        },
        children: [
            {
                path: '/bim/bim',
                component: bim,
                name: 'bim',
                meta: {
                    auth: true
                },
            }, {
                path: '/bim/roam',
                component: roam,
                name: 'roam',
                meta: {
                    auth: true
                },
            }
        ]
    }, {
        path: '/pc',
        component: pc,
        name: 'pc',
        meta: {
            auth: true
        },
        children: [{
            path: '/pc/model',
            component: model,
            name: 'model',
            meta: {
                auth: true
            },
        },
        {
            path: '/pc/marker',
            component: marker,
            name: 'marker',
            meta: {
                auth: true
            },
        },
        {
            path: '/pc/schedule',
            component: schedule,
            name: 'schedule',
            meta: {
                auth: true
            },
        },
        ]
    }, {
        path: '/ibe',
        component: ibe,
        name: 'ibe',
        meta: {
            auth: true
        },
        children: [{
            path: '/ibe/machinery',
            component: machinery,
            name: 'machinery',
            meta: {
                auth: true
            },
        },
        {
            path: '/ibe/robot',
            component: robot,
            name: 'robot',
            meta: {
                auth: true
            },
        },
        ]
    }, {
        path: '/idd',
        component: idd,
        name: 'idd',
        meta: {
            auth: true
        },
    }]
}, {
    path: '/unpermission',
    name: 'unpermission',
    component: unpermission
}]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

router.beforeEach(async (to, from, next) => {

    linkAnalysis(to, next);

    if (to.meta.auth) {
        if (!getLocalStorage('userInfo')) {
            router.push('/login')
        } else {
            next()
        }
    } else {
        next()
    }
})
export default router; //向外共享路由实例对象


//拦截路由，进行地址分析
const linkAnalysis = function (to, next) {

    //获取query值进行解析
    const {
        type, //weburl 表示提供第三方链接
        unionId,
        token
    } = to.query;

    if (type === 'weburl') {

        const path = to.path;

        //判断是否携带 unionId token

        //当前只针对安全页面进行外链
        if (!unionId || token !== '64e20a0610f8bfa24f325f0f' || path !== '/pm/safety') {
            next('/unpermission');
            return
        }
        GLOBAL.LINKTYPE = "weburl";
        GLOBAL.LINKUNIONID = unionId;
        GLOBAL.LINKTOKEN = token;
        to.meta.auth = false;


        //针对不同的页面进行样式调整
        //通过给body添加不同的类名

        window.document.querySelector('body').classList.add(styleTypeMap[path])

    } else if (GLOBAL.LINKTYPE !== "") {
        GLOBAL.LINKTYPE = "";
        GLOBAL.LINKUNIONID = "";
        GLOBAL.LINKTOKEN = "";
    }


}

//样式类名映射
const styleTypeMap = {
    "/pm/safety": '_link_safety_'
}
